#include "bsp.h"

int32_t SystemTimerCnt;
//初始化TIM4 16位定时器，用于做系统的时钟。
void Initial_System_Timer(void){

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);  ///使能TIM4时钟

  TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF; 	//自动重装载值，可计数到1.1930464711小时，即是71.582788266分钟
	TIM_TimeBaseInitStructure.TIM_Prescaler= 84-1;  //分出 1M 的时钟 保证每个周期为1us
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//初始化TIM4

	TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM4中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

	TIM_Cmd(TIM4,ENABLE);  //使能TIMx外设
}

/**
* @brief  	TIM1s时钟初始化
* @param 		arr：自动重装值
						psc：时钟预分频数
* @return   None
*/
void Initial_TIM1(u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);  ///使能TIM1时钟
	
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数设置
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//初始化TIM1
	
	TIM_ClearFlag(TIM1,TIM_FLAG_Update);   				//必须先清除配置时候产生的更新标志
	TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);   		//使能中断，中断事件为定时器工薪事件
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM10_IRQn;	 	//定时器3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; 	//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=2; 					//子优先级2
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void TIM1_UP_TIM10_IRQHandler(void)                //TIM1中断函数
{
  TIM_ClearFlag(TIM1,TIM_FLAG_Update);						//进入中断先清除更新标志
	static int t = 0;
	if(t<500)t++;
	else if(t == 500)
	{
		t=0;
		GPIO_ToggleBits(GPIOB,GPIO_Pin_9);						//每隔500ms翻转一次PB9
	}
}

void TIM4_IRQHandler(void)   													//TIM4中断函数
{
	if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
		{
			SystemTimerCnt++;
			TIM_ClearITPendingBit(TIM4, TIM_IT_Update);  //清除TIMx的中断待处理位:TIM 中断源
		}
}

int32_t Get_SysterTime(void)
{
	return TIM4->CNT + SystemTimerCnt * 0xffff;
}


unsigned char UpdataTimeStamp(TimeStamp *_ptemp)
{
	u32 now_time;
	//系统时间的获取
	if(_ptemp->last_time == 0)
	{
		_ptemp->last_time = micros();
		return 1;
	}
	now_time = micros();

	if(now_time < _ptemp->last_time)
	{
	  	_ptemp->dt = (float)(now_time + (0xFFFFFFFF - _ptemp->last_time) );
	}
	else
	{
		_ptemp->dt = (float)(now_time - _ptemp->last_time);
	}

  	_ptemp->last_time = now_time ;

	_ptemp->dt /= 1000000.0f;

	return 0;

}

//用来测RunTime(StartCheck)和RunTime(StopCheck)间的运行时间，单位为1us
void RunTime(unsigned char flag)
{
	static u32 last_time = 0,now_time = 0;
  u32 dt;

	if(flag == StartCheck)
		last_time = micros(); //读取系统时间
	else if(flag == StopCheck)
	{
		now_time = micros(); //读取系统时间
		if(now_time < last_time)
		{
			dt = (now_time + (0xFFFFFFFF - last_time) );
		}
		else
		{
			dt = (now_time - last_time);
		}
		printf("%d\r\n", dt);
	}

}

void delay_ms_nos(uint32_t cnt)
{
	u32 temp = cnt * 1000 + micros();
	while(temp >= micros());
}

void delay_us_nos(uint32_t cnt)
{
	u32 temp = cnt  + micros();

	while(temp >= micros());
}

